package com.scalar.forecast.forecastSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastSetup.ForecastEntryField;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import com.scalar.forecast.forecastSetup.forms.ForecastSetupForm;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * User: Sujan Kumar
 * Date: Feb 8, 2005
 * Time: 6:07:40 PM
 */
public class ForecastSetupNewCommand extends ActionCommand
{
    public ActionForward execute( ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response )
    {
        HashMap errors = new HashMap ();
        boolean success = false;
        try
        {
            ForecastSetupForm forecastSetupForm = ( ForecastSetupForm )form;
            if ( forecastSetupForm == null )
            {
                forecastSetupForm = new ForecastSetupForm();
            }

            //start: setting forecast Setup Fields in the form.
            ForecastEntryField forecastSetupField = null;
            ArrayList forecastSetupFields = ForecastSetupDAO.getForecastSetupFields(null);
            int listSize    = forecastSetupFields.size();
            for ( int i=0; i < listSize; i++ )
            {
                forecastSetupField = ( ForecastEntryField )forecastSetupFields.get( i );
                String setupFieldName = forecastSetupField.getEntryName();
                if ( "By Product".equalsIgnoreCase( setupFieldName ) )
                {
                    forecastSetupForm.setByProduct( forecastSetupField.isEntrySelected() );
                }
                else if ( "By Customer".equalsIgnoreCase( setupFieldName ) )
                {
                    forecastSetupForm.setByCustomer( forecastSetupField.isEntrySelected() );
                }
                else if ( "By Project".equalsIgnoreCase( setupFieldName ) )
                {
                    forecastSetupForm.setByProject( forecastSetupField.isEntrySelected() );
                }
                else if ( "By ODM".equalsIgnoreCase( setupFieldName ) )
                {
                    forecastSetupForm.setByOdm( forecastSetupField.isEntrySelected() );
                }
            }
            //end of setting forecast setup feilds.
            //start: setting forecast TimeFrame Fields in the form.
            ForecastTimeFrame forecastTimeFrame =ForecastSetupDAO.getForecastTimeFrame(null);
            forecastSetupForm.setTfName( forecastTimeFrame.getTfName() );
            forecastSetupForm.setTfCompany( forecastTimeFrame.getTfCompany()  );
            forecastSetupForm.setTfMeasureType( forecastTimeFrame.getTfMeasureType() );
            //end of setting forecast TimeFrame Fields in the form.
            //start: setting forecast levles in the form.
            ArrayList levels = ForecastSetupDAO.getForecastLevels(null);
            if ( levels.size() != 0 )
            {
                forecastSetupForm.setLevels( levels );
            }
            //end of setting forecast levles in the form.
            //start of setting forecast entry fields in the form.
            forecastSetupForm.setForecastEntryFields( ForecastSetupDAO.getForecastEntryFields(null) );
            //end of setting forecast entry fields in the form.
            success = true;
        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_FORECAST_SETUP_SCREEN );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "-----------Exception in ForecastSetupNewCommand----------");
            e.printStackTrace();
            return mapping.findForward ( "errorPage" );
        }
        ActionForward actionForward = null;
        actionForward = mapping.findForward("forecastSetup");
        return actionForward;
    }
}
